#ifndef BMP280DRIVER_H
#define BMP280DRIVER_H

#include "bsp_iic.h"
#include "main.h"
#include "bsp_dwt.h"
#include "bmp280Reg.h"
#include "stdint.h"

typedef struct
{
    uint16_t dig_T1;
    int16_t dig_T2;
    int16_t dig_T3;
    uint16_t dig_P1;
    int16_t dig_P2;
    int16_t dig_P3;
    int16_t dig_P4;
    int16_t dig_P5;
    int16_t dig_P6;
    int16_t dig_P7;
    int16_t dig_P8;
    int16_t dig_P9;
} Trimming_parameter_s;


typedef struct
{
    IIC_Init_Config_s iic_conf;                     // iic配置
    uint8_t id;                                     // 设备ID
    BMP280_WORK_MODE_e work_mode;                   // 工作模式
    BMP280_P_OVERSAMPLING_e p_oversampling;         // 气压过采样率
    BMP280_T_OVERSAMPLING_e t_oversampling;         // 温度过采样率
    BMP280_FILTER_COEFFICIENT_e filter_coefficient; // 过滤器设置
    BMP280_T_SB_e t_sb;                           // 休眠时间
} BMP280Driver_conf_s;

typedef struct
{
    float temperature; // 温度值
    float pressure;    // 气压值
    double t_fine;   // 温度补偿值
    double p_fine;   // 气压补偿值
    uint8_t status;    // 状态值
} BMP280_measure_s;

typedef struct
{
    IICInstance *iic_instance;                      // iic实例
    uint8_t id;                                     // 设备ID
    BMP280_measure_s measure;                       // 测量值
    BMP280_WORK_MODE_e work_mode;                   // 工作模式
    BMP280_P_OVERSAMPLING_e p_oversampling;         // 气压过采样率
    BMP280_T_OVERSAMPLING_e t_oversampling;         // 温度过采样率
    BMP280_FILTER_COEFFICIENT_e filter_coefficient; // 过滤器设置
    BMP280_T_SB_e t_sb;                           // 休眠时间
    Trimming_parameter_s trimming_parameter; // 校准参数
} BMP280DriverInstance_s;
BMP280DriverInstance_s *BMP280Driver_Init(BMP280Driver_conf_s *conf);
void BMP280WriteByte(BMP280DriverInstance_s *bmp280, uint8_t reg, uint8_t value);
void BMP280ReadByte(BMP280DriverInstance_s *bmp280, uint8_t reg, uint8_t *data, uint16_t size);
void BMP280SetStandbyFilter(BMP280DriverInstance_s *bmp280, BMP280_FILTER_COEFFICIENT_e filter_coefficient, BMP280_T_SB_e t_sb);
void BMP280Calibrate(BMP280DriverInstance_s *bmp280);
void BMP280SetOversamplingAndMode(BMP280DriverInstance_s *bmp280, BMP280_P_OVERSAMPLING_e p_oversampling, BMP280_T_OVERSAMPLING_e t_oversampling, BMP280_WORK_MODE_e work_mode);
void BMP280GetID(BMP280DriverInstance_s *bmp280, uint8_t *id);
void BMP280Reset(BMP280DriverInstance_s *bmp280);
void BMP280GetPressure(BMP280DriverInstance_s *bmp280, float *pressure);
void BMP280GetTemperature(BMP280DriverInstance_s *bmp280, float *temperature);
#endif // BMP280DRIVER_H